package com.base.cn.platform.os.service.manage.user;

import com.base.cn.platform.os.common.md5.MD5;
import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.mybatis.BaseEntity;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.user.UserExpandFrom;
import com.base.cn.platform.os.dao.manage.user.SysUserDao;
import com.base.cn.platform.os.entity.manage.function.SysFunction;
import com.base.cn.platform.os.entity.manage.group.SysGroup;
import com.base.cn.platform.os.entity.manage.join.SysUserRole;
import com.base.cn.platform.os.entity.manage.role.SysRole;
import com.base.cn.platform.os.entity.manage.user.SysUser;
import com.base.cn.platform.os.entity.manage.user.SysUserForm;
import com.base.cn.platform.os.entity.manage.user.SysUserFunction;
import com.base.cn.platform.os.entity.user.user.CusUser;
import com.base.cn.platform.os.service.manage.function.SysFunctionBiz;
import com.base.cn.platform.os.service.manage.group.SysGroupBiz;
import com.base.cn.platform.os.service.manage.join.SysUserRoleBiz;
import com.base.cn.platform.os.service.manage.login.SysLoginLogBiz;
import com.base.cn.platform.os.service.manage.role.SysRoleBiz;
import com.base.cn.platform.os.service.manage.teacher.SysTeacherBiz;
import com.base.cn.platform.os.service.user.user.CusUserBiz;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户业务类
 *
 * @author s.li
 * @create 2018-02-05 16:30
 */
@Service
public class SysUserBiz extends BaseBiz<SysUser,SysUserDao> {

    @Autowired
    private SysUserRoleBiz sysUserRoleBiz;
    @Autowired
    private SysRoleBiz sysRoleBiz;
    @Autowired
    private SysLoginLogBiz sysLoginLogBiz;
    @Autowired
    private SysGroupBiz sysGroupBiz;
    @Autowired
    private SysTeacherBiz sysTeacherBiz;
    @Autowired
    private CusUserBiz cusUserBiz;
    @Autowired
    private SysFunctionBiz sysFunctionBiz;
    @Autowired
    private SysUserFunctionBiz sysUserFunctionBiz;

    /**
     * 通过用户ID串，查询用户数据Map
     * @param userIds 用户ID串
     * @return Map
     */
    public Map<BigDecimal,SysUser> findSysUserByIds(String userIds){
        List<SysUser> sysUserList = findSysUserByUserIds(userIds);
        if(ObjectUtils.isNotEmpty(sysUserList)){
            return sysUserList.stream().collect(Collectors.toMap(e->e.getId(),e->e));
        }
        return null;
    }

    /**
     * 通过用户ID串，获取用户Map
     * @param userIds 用户ID串
     * @return Map<BigDecimal,SysUser>
     */
    public Map<BigDecimal,SysUser> findSysUserMapByUserIds(String userIds,
                                                           boolean roleData,
                                                           boolean groupData,
                                                           boolean createUser){
        List<SysUser> sysUserList = findSysUserByUserIds(userIds);
        if(ObjectUtils.isNotEmpty(sysUserList)){
            sysUserList = this.setSysUserExtendData(sysUserList,roleData,groupData,createUser);
            return sysUserList.stream().collect(Collectors.toMap(e->e.getId(),e->e));
        }
        return null;
    }

    /**
     * 通过用户ID串，获取用户列表
     * @param userIds 用户ID串
     * @return List<SysUser>
     */
    public List<SysUser> findSysUserByUserIds(String userIds){
        if(StringUtils.isNotEmpty(userIds)){
            userIds = StringUtils.subHeadTailString(userIds,",");
            if(StringUtils.isNotEmpty(userIds)){
                List<SysUser> sysUserList = find(" id in ("+userIds+")",null,null);
                return sysUserList;
            }
        }
        return null;
    }

    /**
     * 通过用户ID，查询用户对象
     * @param id 用户对象
     * @param roleData 设置用户的角色数据
     * @param groupData 设置用户的部门数据
     * @param createUser 设置用户的创建对象
     * @return SysUser
     */
    public SysUser findSysUserById(BigDecimal id,
                                   boolean roleData,
                                   boolean groupData,
                                   boolean createUser ){
        SysUser sysUser = null;
        if(ObjectUtils.isNotEmpty(id) && id.compareTo(new BigDecimal(0))>0){
            List<SysUser> sysUserList = findSysUserByUserIds(id.toString());
            if(ObjectUtils.isNotEmpty(sysUserList)){
                sysUser = this.setSysUserExtendData(sysUserList.get(0),roleData,groupData,createUser);
            }
        }
        return sysUser;
    }

    /**
     * 分页查询管理员用户列表
     * @param page 分页条件
     * @param sysUser 查询条件
     * @param roleData 是否查询角色数据
     * @param groupData 是否查询部门数据
     * @param createUser 是否查询创建用户数据
     * @return 当前用户数据
     */
    public PageInfo<SysUser> querySysUserList(Pagination page, SysUser sysUser,
                                              boolean roleData,boolean groupData,boolean createUser){
        StringBuilder whereSql= new StringBuilder("  id!=1 and status !=3");
        String userIds = "";
        if(ObjectUtils.isNotEmpty(sysUser)){
            if(StringUtils.isNotEmpty(sysUser.getRoleIds())){
                List<SysUserRole> sysUserRoleList = sysUserRoleBiz.find(" roleId="+sysUser.getRoleIds() +" and userId !=1",null,null);
                if(ObjectUtils.isEmpty(sysUserRoleList)){
                    return new PageInfo<SysUser>();
                }
                userIds = sysUserRoleList.stream().map(e->e.getUserId().toString()).collect(Collectors.joining(","));
            }
            if(StringUtils.isNotEmpty(sysUser.getUsername())){
                whereSql .append(" and username like '%").append(sysUser.getUsername().trim()+"%' ").append(
                           " or mobile like '%").append(sysUser.getUsername().trim()).append("%'");
            }
            if(StringUtils.isNotEmpty(sysUser.getMobile())){
                whereSql .append(" and mobile like '%").append(sysUser.getMobile().trim()).append("%'");
            }
            if(sysUser.getGroupId()!=null && sysUser.getGroupId().compareTo(new BigDecimal(0))>0){
                whereSql .append(" and groupIds like '%,").append(sysUser.getGroupId()).append(",%'");
            }
            if(sysUser.getUserType()!=null && sysUser.getUserType().intValue()>0){
                whereSql .append(" and userType =").append(sysUser.getUserType());
            }
            if(sysUser.getNotTeacher()!=null && sysUser.getNotTeacher().booleanValue()==true){
                whereSql .append(" and (teacherId =0 or teacherId is null)");
            }
        }
        if(StringUtils.isNotEmpty(userIds)){
            whereSql .append(" and id in (").append(userIds).append(")");
        }
        whereSql .append(" order by createTime desc");
        PageInfo<SysUser> sysUserPageInfo = this.findPage(whereSql.toString(),page,null);
        sysUserPageInfo.setList(this.setSysUserExtendData(sysUserPageInfo.getList(),roleData,groupData,createUser));
        return sysUserPageInfo;
    }

    /**
     * 创建管理员
     * @param sysUser 管理数据对象
     * @param sysUserForm 页面传递的数据
     * @return Map<String,Object>
     */
    @Transactional(rollbackFor =  IllegalArgumentException.class)
    public SysUser createSysUser(SysUser sysUser,SysUserForm sysUserForm){
        if(DataUtil.idIsNotNull(sysUser.getId())){//修改
            SysUser _sysUser = this.findById(sysUser.getId());
            _sysUser.setRalName(sysUser.getRalName());
            _sysUser.setMobile(sysUser.getMobile());
            _sysUser.setImageJson(sysUser.getImageJson());
            _sysUser.setGroupId(sysUser.getGroupId());
            _sysUser.setGroupIds(sysUser.getGroupIds());
            this.updateById(_sysUser,null);//修改用户
            if(_sysUser.getUserType()==1){
                //创建角色
                this.createSysUserRoleData(sysUser.getId(),sysUserForm.getRoleIds());
            }
        }else{//添加
            this.save(sysUser);//保存用户
            if(sysUserForm.getRegType()==1){//如果是后台创建的
                //是否同步前台用户
                String sync = sysUserForm.getIsSync();
                if(StringUtils.isNotEmpty(sync) && "ON".equals(sync.toUpperCase())){//创建前台用户
                    if(ObjectUtils.isEmpty(cusUserBiz.queryUserByMobile(sysUser.getMobile()))){
                        CusUser cusUser = new CusUser();
                        String loginAccount = sysUser.getUsername();
                        String password = sysUser.getMobile();
                        String mobile = sysUser.getMobile();
                        cusUser.setLoginAccount(loginAccount);
                        cusUser.setPassword(password.substring(password.length() -  6));
                        cusUser.setMobile(mobile);
                        cusUser.setMobileIsavalible(1);
                        cusUser.setRegisterFrom(UserExpandFrom.adminFrom.toString());
                        cusUserBiz.createUser(cusUser);
                    }
                }
                //是否同步讲师
                String isTeacher = sysUserForm.getIsTeacher();
                if(StringUtils.isNotEmpty(isTeacher) && "ON".equals(isTeacher.toUpperCase())){//创建讲师
                    //创建账户关联的讲师
                    BigDecimal teacherId = sysTeacherBiz.sysSysUserToTeacher(sysUser);
                    sysUser.setTeacherId(teacherId);
                    SysUser sysUser1 = new SysUser();
                    sysUser1.setTeacherId(teacherId);
                    this.updateByWhereSql(sysUser1," id="+sysUser.getId());
                }

            }
            //创建角色
            this.createSysUserRoleData(sysUser.getId(),sysUserForm.getRoleIds());
            StringBuilder whereSql = new StringBuilder();
            whereSql.append("functionName='基础管理' or functionName='工作台' or functionName='待办事项'");
            whereSql.append(" or id = 302 or id = 327 or id =328 or id =329");
            whereSql.append(" or functionUrl='/admin/manage/work/myWorkbench' or functionUrl ='/admin/manage/work/myBacklog'");
            List<SysFunction> sysFunctionList = sysFunctionBiz.find(whereSql.toString(),null,Arrays.asList("id"));
            if(ObjectUtils.isNotEmpty(sysFunctionList)){
                List<SysUserFunction> userFunctionList = new ArrayList<>();
                sysFunctionList.forEach(e->{
                    SysUserFunction userFunction = new SysUserFunction();
                    userFunction.setUserId(sysUser.getId());
                    userFunction.setFunctionId(e.getId());
                    userFunctionList.add(userFunction);
                });
                sysUserFunctionBiz.batchSave(userFunctionList);
            }
        }
        return sysUser;
    }

    /**
     * 通过用户名查询用户
     * @param username 用户名
     * @return 管理员用户对象
     */
    public SysUser findSysUserByUsername(String username){
        return findSysUserByUsername(username,null);
    }

    /**
     * 通过用户名查询用户
     * @param username 用户名
     * @param notUserId 排除的用户ID
     * @return 管理员用户对象
     */
    public SysUser findSysUserByUsername(String username,BigDecimal notUserId){
        if(StringUtils.isNotEmpty(username)){
            String whereSql = " (username ='"+username.trim()+"' or mobile='"+username.trim()+"')";
            if(ObjectUtils.isNotEmpty(notUserId) && notUserId.compareTo(new BigDecimal(0))>0){
                whereSql =" id !="+notUserId+" and"+whereSql;
            }
            return this.findOne(whereSql,null);
        }
        return null;
    }

    /**
     * 通过手机号，查询用户账号
     * @param mobile 手机号
     * @return 用户账号对象
     */
    public SysUser findSysUserByMobile(String mobile){
        return this.findSysUserByMobile(mobile,null);
    }

    /**
     * 通过手机号，查询用户账号
     * @param mobile 手机号
     * @param notUserId  要排除的用户ID
     * @return 用户账号对象
     */
    public SysUser findSysUserByMobile(String mobile,BigDecimal notUserId){
        if(StringUtils.isNotEmpty(mobile)){
            String whereSql = " mobile ='"+mobile.trim()+"'";
            if(ObjectUtils.isNotEmpty(notUserId) && notUserId.compareTo(new BigDecimal(0))>0){
                whereSql =" id !="+notUserId+" and"+whereSql;
            }
            return this.findOne(whereSql,null);
        }
        return null;

    }

    /**
     * 把SysUserForm转成SysUser
     * @param sysUserForm 添加用户的数据
     * @return 处理后的用户对象
     */
    public SysUser formToSysUser(SysUserForm sysUserForm){
        SysUser sysUser = new SysUser();
        sysUser.setId(sysUserForm.getId());
        if(sysUserForm.getRegType()==1){//后台开通账号
            sysUser.setUsername(sysUserForm.getAccount());
            sysUser.setMobile(sysUserForm.getMobile());
            sysUser.setPassword(MD5.getMD5("123456"));//设置默认密码
            sysUser.setImageJson(sysUserForm.getUserImg());//用户头像
            if(sysUserForm.getUserType()==1){//1开通正式账号
                sysUser.setGroupIds(sysUserForm.getGroupIds());
                sysUser.setGroupId(sysUserForm.getGroupId());
                sysUser.setStatus(1);//设置状态为正常
            }else if(sysUserForm.getUserType()==2){//开通体验账号
                sysUserForm.setRoleIds("4");//体验账号固有的角色
                sysUser.setStatus(0);//设置状态为审核未通过
            }
            sysUser.setUserType(sysUserForm.getUserType());//设置用户类型
            sysUser.setRalName(sysUserForm.getRalName());//设置真名
            sysUser.setCreateUserId(sysUserForm.getCreateUserId());//设置创建人ID
        }else if(sysUserForm.getRegType()==2){//页面申请体验账号
            sysUser.setUsername(sysUserForm.getAccount().trim());
            sysUser.setMobile(sysUserForm.getAccount().trim());
            sysUser.setRalName(sysUserForm.getAccount().trim());
            sysUser.setPassword(MD5.getMD5(sysUserForm.getPassword()));
            sysUser.setUserType(2);//体验账号类型
            sysUser.setStatus(0);// 未授权
            sysUserForm.setRoleIds("4");//体验账号固有的角色
        }
        sysUser.setGuideStatus(0);
        return sysUser;
    }

    /**
     * 创建用户的角色关联数据
     * @param sysUserId 用户ID
     * @param roleIds 角色ID串
     */
    public void createSysUserRoleData(BigDecimal sysUserId,String roleIds){
        if(ObjectUtils.isNotEmpty(sysUserId) && StringUtils.isNotEmpty(roleIds)){
            roleIds = StringUtils.subHeadTailString(roleIds,",");
            sysUserRoleBiz.createSysUserRole(sysUserId,roleIds);
        }
    }

    /**
     * 修改用户状态
     * @param userIds 用户ID串
     * @param status 状态，1正常，2禁用，3删除
     */
    public void updateSysUserStatus(String userIds ,int status){
        if(StringUtils.isNotEmpty(userIds)){
            userIds = StringUtils.subHeadTailString(userIds,",");
            if(StringUtils.isNotEmpty(userIds)){
                SysUser sysUser = new SysUser();
                sysUser.setStatus(status);
                this.updateByWhereSql(sysUser," id in ("+userIds+")");
            }
        }
    }

    /**
     * 删除用户
     * @param userIds 用户ID串
     */
    @Transactional(rollbackFor = IllegalArgumentException.class)
    public void deleteSysUser(String userIds){
        if(StringUtils.isNotEmpty(userIds)){
            userIds = StringUtils.subHeadTailString(userIds,",");
            if(StringUtils.isNotEmpty(userIds)){
                this.deleteWhereSql("id in ("+userIds+")");
                sysUserRoleBiz.deleteWhereSql("userId in ("+userIds+")");
                sysLoginLogBiz.deleteWhereSql("userId in ("+userIds+")");
            }
        }
    }

    /**
     * 延期体验用户过期时间（自动延期7天）
     * @param userIds 用户ID
     */
    public Date delayedUser(String userIds){
        if(StringUtils.isNotEmpty(userIds)){
            userIds = StringUtils.subHeadTailString(userIds,",");
            if(StringUtils.isNotEmpty(userIds)){
                Date endTime = new Date();
                endTime = DateUtils.addDays(endTime,7);
                SysUser sysUser = new SysUser();
                sysUser.setEndTime(endTime);
                this.updateByWhereSql(sysUser,"id in ("+userIds+") and userType = 2");
                return endTime;
            }
        }
        return null;
    }

    /**
     * 修改用户密码
     * @param userId 用户ID
     * @param newPassword 用户新密码
     */
    public void updateSysUserPwd(BigDecimal userId, String newPassword) {
        SysUser sysUser = new SysUser();
        sysUser.setPassword(MD5.getMD5(newPassword));
        this.updateByWhereSql(sysUser," id =" + userId);
    }

    //================================
    /**
     * 设置用户扩展数据
     * @param sysUser 用户对象
     * @param roleData 是否查询角色数据
     * @param groupData 是否查询部门数据
     * @param createUser 是否查询创建用户数据
     * @return List<SysUser>
     */
    private SysUser setSysUserExtendData(SysUser sysUser, boolean roleData, boolean groupData, boolean createUser) {
        if(ObjectUtils.isNotEmpty(sysUser)){
            List<SysUser> sysUserList = new ArrayList<>();
            sysUserList.add(sysUser);
            sysUserList = this.setSysUserExtendData(sysUserList,roleData,groupData,createUser);
            return sysUserList.get(0);
        }
        return sysUser;
    }
        /**
         * 设置用户扩展数据
         * @param sysUserList 用户列表
         * @param roleData 是否查询角色数据
         * @param groupData 是否查询部门数据
         * @param createUser 是否查询创建用户数据
         * @return List<SysUser>
         */
    private List<SysUser> setSysUserExtendData(List<SysUser> sysUserList, boolean roleData, boolean groupData, boolean createUser) {
        if(ObjectUtils.isNotEmpty(sysUserList)){
            if(roleData){//查询角色数据
                sysUserList = this.setSysUserRoleData(sysUserList,roleData);
            }
            if(createUser){//查询创建用户数据
                sysUserList = this.setCreateUserData(sysUserList,createUser);
            }
            if(groupData){//查询部门数据
                sysUserList = this.setGroupData(sysUserList,groupData);
            }
        }
        return sysUserList;
    }

    /**
     * 设置用户的角色数据
     * @param sysUserList 用户列表
     * @param roleData 是否查询角色状态
     * @return List<SysUser>
     */
    private List<SysUser> setSysUserRoleData(List<SysUser> sysUserList,boolean roleData){
        if(ObjectUtils.isNotEmpty(sysUserList) && roleData){
            String userIds = sysUserList.stream().map(e->e.getId().toString()).collect(Collectors.joining(","));
            //获取用户与角色的关联数据
            List<SysUserRole> sysUserRoleList = sysUserRoleBiz.findSysUserRoleListByUserId(userIds);
            if(ObjectUtils.isNotEmpty(sysUserRoleList)){
                Map<BigDecimal,List<SysUserRole>> sysUserRoleListMap = sysUserRoleList.stream().collect(Collectors.groupingBy(e->e.getUserId()));

                String roleIds = sysUserRoleList.stream().map(e->e.getRoleId().toString()).collect(Collectors.joining(","));
                //获取角色列表
                List<SysRole> sysRoleList = sysRoleBiz.findSysRoleListByIds(roleIds,false,false);
                if(ObjectUtils.isNotEmpty(sysRoleList)){
                    Map<BigDecimal,SysRole> sysRoleMap = sysRoleList.stream().collect(Collectors.toMap(e->e.getId(),e->e));
                    sysUserList.forEach(e->{
                        List<SysUserRole> sysUserRoles = sysUserRoleListMap.get(e.getId());
                        List<SysRole>  usRoleList = new ArrayList<SysRole>();
                        if(ObjectUtils.isNotEmpty(sysUserRoles)){
                            sysUserRoles.forEach(em->{
                                SysRole sysRole = sysRoleMap.get(em.getRoleId());
                                if(ObjectUtils.isNotEmpty(sysRole)){
                                    usRoleList.add(sysRole);
                                }
                            });
                        }
                        if(!usRoleList.isEmpty()){
                            e.setSysRoleList(usRoleList);
                            String userRoleIds = usRoleList.stream().map(m->m.getId().toString()).collect(Collectors.joining(",",",",","));
                            e.setRoleIds(userRoleIds);
                        }
                    });
                }
            }

        }
        return sysUserList;
    }

    /**
     * 设置用户的创建用户数据
     * @param sysUserList 用户列表
     * @param createUser 是否获取创建用户的数据
     * @return List<SysUser>
     */
    private List<SysUser> setCreateUserData(List<SysUser> sysUserList, boolean createUser) {
        if(ObjectUtils.isNotEmpty(sysUserList) && createUser){
            String userIds = sysUserList.stream().filter(e->e.getCreateUserId().compareTo(new BigDecimal(0))>0).map(e->e.getCreateUserId().toString()).collect(Collectors.joining(","));
            if(StringUtils.isNotEmpty(userIds)){
                Map<BigDecimal,SysUser> userMap = this.findSysUserByIds(userIds);
                if(ObjectUtils.isNotEmpty(userMap)){
                    sysUserList.forEach(e->{
                        SysUser _createUser = userMap.get(e.getCreateUserId());
                        if(ObjectUtils.isNotEmpty(_createUser)){
                            e.setSysCreateUser(_createUser);
                        }
                    });
                }
            }
        }
        return sysUserList;
    }

    /**
     * 设置用户的部门数据
     * @param sysUserList 用户列表
     * @param groupData 是否查询部门数据
     * @return List<SysUser>
     */
    private List<SysUser> setGroupData(List<SysUser> sysUserList, boolean groupData) {
        if(ObjectUtils.isNotEmpty(sysUserList) && groupData){
            String groupIds = sysUserList.stream().filter(e->StringUtils.isNotEmpty(e.getGroupIds())).map(e->e.getGroupIds()).collect(Collectors.joining());
            groupIds = StringUtils.subHeadTailString(groupIds,",");
            groupIds = groupIds.replaceAll(",,",",");
            if(StringUtils.isNotEmpty(groupIds)){
                List<SysGroup> sysGroupList = sysGroupBiz.find(" id in("+groupIds+") and status = 1",null,null);
                Map<BigDecimal,SysGroup> sysGroupMap = sysGroupList.stream().collect(Collectors.toMap(e -> e.getId(),e -> e));
                if(ObjectUtils.isNotEmpty(sysGroupMap)){
                    sysUserList.forEach(e->{
                        String _groupIds = e.getGroupIds();
                        _groupIds= StringUtils.subHeadTailString(_groupIds,",");
                        if(StringUtils.isNotEmpty(_groupIds)){
                            String[] arr = _groupIds.split(",");
                            List<SysGroup> userGroupList = new ArrayList<SysGroup>();
                            Arrays.stream(arr).forEach(a->{
                                if(StringUtils.isNotEmpty(a)){
                                    SysGroup sysGroup = sysGroupMap.get(new BigDecimal(a));
                                    if(ObjectUtils.isNotEmpty(sysGroup)){
                                        userGroupList.add(sysGroup);
                                    }
                                }
                            });
                            if(ObjectUtils.isNotEmpty(userGroupList)){
                                e.setSysGroupList(userGroupList);
                            }
                        }
                        SysGroup sysGroup = sysGroupMap.get(e.getGroupId());
                        if(ObjectUtils.isNotEmpty(sysGroup)){
                            e.setSysGroup(sysGroup);
                        }
                    });
                }
            }
        }
        return sysUserList;
    }

    /**
     * 通过用户名查询id串
     * @param username
     * @return
     */
    public String getUserNameIds(String username) {
        List<String> ids = null;
        if (StringUtils.isNotEmpty(username)) {
            List<SysUser> sysUsers = this.find("username like concat('%','"+username+"','%')", null, null);
            if (ObjectUtils.isNotEmpty(sysUsers)) {
                ids = new ArrayList<String>();
                for (SysUser user : sysUsers) {
                    ids.add(user.getId().toString());
                }
                return  StringUtils.collectionToDelimitedString(ids,",");
            }
        }
        return  null;
    }
    /**
     * 设置创建人员数据
     * @param baseEntity 数据对象
     * @param sysUserData 是否设置创建人员数据，true设置，false不设置
     */
    public void setSysUserData(BaseEntity baseEntity, boolean sysUserData){
        List<BaseEntity> dataList = new ArrayList<>();
        dataList.add(baseEntity);
        this.setSysUserData(dataList, sysUserData);
    }

    /**
     * 设置创建人员数据
     * @param list 数据列表
     * @param sysUserData 是否设置创建人员数据，true设置，false不设置
     */
    public void setSysUserData(List<? extends BaseEntity> list, boolean sysUserData){
        if(ObjectUtils.isNotEmpty(list) && sysUserData){
            String userIds = list.stream().filter(e-> DataUtil.idIsNotNull(e.getCreateUserId())).map(e->e.getCreateUserId().toString()).collect(Collectors.joining(","));
            Map<BigDecimal,SysUser> userMaps = this.findSysUserMapByUserIds(userIds,false,false,false);
            if(ObjectUtils.isNotEmpty(userMaps)){
                list.forEach(e->{
                    SysUser sysUser = userMaps.get(e.getCreateUserId());
                    if(ObjectUtils.isNotEmpty(sysUser)){
                        Map<String,Object> userMap = new HashMap<>();
                        userMap.put("id",sysUser.getId());
                        userMap.put("username",sysUser.getUsername());
                        userMap.put("ralName",sysUser.getRalName());
                        userMap.put("mobile",sysUser.getMobile());
                        userMap.put("imageMap",sysUser.getImageMap());
                        e.setCreateUser(userMap);
                    }
                });
            }
        }
    }
}
